package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserOreWarPatronsData;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserOreWarPatronsDaoImpl extends TempDataObjectDao<UserOreWarPatronsData> {

    public Map<Integer, UserOreWarPatronsData> getUserOreWarPatronsDataMap(long userId,int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_activity_orewar_patrons WHERE userId=? AND activityId = ?";
        Map<Integer, UserOreWarPatronsData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i =1;
            call.setLong(i++, userId);
            call.setLong(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserOreWarPatronsData data = parse(rs);
                dataMap.put(data.getPatronsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserOreWarPatronsData data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_activity_orewar_patrons(userId,patronsId,battleTimes,buyBattleTimes,dispatchOre,modifyTime,activityId,pitId) VALUES(?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPatronsId());
            call.setInt(i++, data.getBattleTimes());
            call.setInt(i++, data.getBuyBattleTimes());
            call.setInt(i++, data.getDispatchOre());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getPitId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserOreWarPatronsData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_activity_orewar_patrons SET battleTimes=?,buyBattleTimes=?,dispatchOre=?,modifyTime=?,pitId=? WHERE userId=? AND patronsId=? AND activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getBattleTimes());
            call.setInt(i++, data.getBuyBattleTimes());
            call.setInt(i++, data.getDispatchOre());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getPitId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPatronsId());
            call.setInt(i++, data.getActivityId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public boolean updateUserOreWarPatronsData(long userId, List<Integer> patronsIdList, int dispatchOre, int activityId, int pitId) {
        boolean result = false;
        String sql = "UPDATE t_u_activity_orewar_patrons SET dispatchOre=?,pitId=? WHERE userId=? and patronsId=? AND activityId=?";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (int patronsId : patronsIdList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, dispatchOre));
            para.put(i++, new DbParameter(Types.INTEGER, pitId));
            para.put(i++, new DbParameter(Types.BIGINT, userId));
            para.put(i++, new DbParameter(Types.INTEGER, patronsId));
            para.put(i++, new DbParameter(Types.INTEGER, activityId));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    @Override
    protected UserOreWarPatronsData parseData(ResultSet rs) throws SQLException {
        UserOreWarPatronsData data = new UserOreWarPatronsData();
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setBattleTimes(rs.getInt("battleTimes"));
        data.setBuyBattleTimes(rs.getInt("buyBattleTimes"));
        data.setDispatchOre(rs.getInt("dispatchOre"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setActivityId(rs.getInt("activityId"));
        data.setPitId(rs.getInt("pitId"));
        return data;
    }
}
